package com.woniuxy.certified.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.certified.dao.ApplicationForm;
import com.woniuxy.certified.dao.CertifiedAccount;
import com.woniuxy.certified.dao.CertifiedInfo;
import com.woniuxy.certified.mapper.ApplicationFormMapper;
import com.woniuxy.certified.mapper.CertifiedAccountMapper;
import com.woniuxy.certified.mapper.CertifiedInfoMapper;
import com.woniuxy.certified.model.dto.CertifiedAccountDto;
import com.woniuxy.certified.model.param.ApplicationParam;
import com.woniuxy.certified.model.param.PassAllApplicationParam;
import com.woniuxy.certified.service.ApplicationFormService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.core.exception.certifiedException.CertifiedException;
import com.woniuxy.core.exception.certifiedException.CertifiedExceptionCode;
import com.woniuxy.core.staticCode.CertifiedCode;
import com.woniuxy.core.util.OSSUtil;
import com.woniuxy.core.util.PageInfo;
import lombok.Data;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class ApplicationFormServiceImpl extends ServiceImpl<ApplicationFormMapper, ApplicationForm> implements ApplicationFormService {

    @Resource
    private ApplicationFormMapper applicationFormMapper;

    @Resource
    private CertifiedInfoMapper certifiedInfoMapper;

    @Resource
    private CertifiedAccountMapper accountMapper;

    @Resource
    private OSSUtil ossUtil;

    @Override
    public void test() {
        System.out.println("测试成功");
        return;
    }

    @Override
    public void applicationForProperty(ApplicationParam param) {
        ApplicationForm applicationForm = BeanUtil.toBean(param, ApplicationForm.class);
        applicationForm.setApplicationStatus(CertifiedCode.UNDER_REVIEW.getCode());
        applicationFormMapper.insert(applicationForm);
    }

    @Override
    public String uploadImage(MultipartFile image) throws IOException {
        //获取文件类型
        String fileType = FileTypeUtil.getType(image.getInputStream());
        if (!fileType.equals("jpg")&&!fileType.equals("png")){
            throw new CertifiedException(CertifiedExceptionCode.PICTURE_FORMAT_ERROR);
        }
        //创建文件名称
        Snowflake snowflake = new Snowflake(1,1);
        String prefix = snowflake.nextIdStr();
        String fileName = prefix+"."+fileType;
        String url = ossUtil.uploadFile(image.getInputStream(), fileName, "car-port");
        return url;
    }

    @Override
    public CertifiedAccountDto passApplication(Integer id) {
        //状态是否为为待审核状态
        ApplicationForm applicationForm = applicationFormMapper.selectById(id);
        if (applicationForm.getApplicationStatus()!=CertifiedCode.UNDER_REVIEW.getCode()){
            throw new CertifiedException(CertifiedExceptionCode.DO_NOT_REPEAT_OPERATION);
        }
        //修改状态
        applicationForm.setApplicationStatus(CertifiedCode.AUDIT_PASS.getCode());
        applicationFormMapper.updateById(applicationForm);
        //生成物业详情表
        CertifiedInfo certifiedInfo = null;
        try {
            certifiedInfo = BeanUtil.toBean(applicationForm, CertifiedInfo.class);
            //设置默认车位价格
            certifiedInfo.setStallUnitPrice(BigDecimal.valueOf(10));
            certifiedInfo.setChargeUnitPrice(BigDecimal.valueOf(10));
        } catch (Exception e) {
            //补偿,修改回状态
            applicationForm.setApplicationStatus(CertifiedCode.UNDER_REVIEW.getCode());
            applicationFormMapper.updateById(applicationForm);
            throw new RuntimeException(e);
        }
        Integer certifiedInfoId = certifiedInfoMapper.insert(certifiedInfo);
        //创建超级管理员账号
        CertifiedAccount certifiedAccount = new CertifiedAccount();
        certifiedAccount.setCertifiedAccountAccount(CertifiedCode.SUPER_ADMIN_ACCOUNT.getMessage()+String.valueOf(certifiedInfo.getCertifiedInfoId()));
        certifiedAccount.setCertifiedAccountPass(CertifiedCode.SUPER_ADMIN_PSS.getMessage());
        certifiedAccount.setAccountRole(CertifiedCode.SUPER_ADMIN.getCode());
        certifiedAccount.setCertifiedAccountState(CertifiedCode.ACCOUNT_ENABLE.getCode());
        certifiedAccount.setCertifiedInfoId(certifiedInfo.getCertifiedInfoId());
        try {
            accountMapper.insert(certifiedAccount);
            //返回生成的超级管理员账号密码
            CertifiedAccountDto dto = BeanUtil.toBean(certifiedAccount, CertifiedAccountDto.class);
            return dto;
        } catch (Exception e) {
            //补偿,修改回状态
            applicationForm.setApplicationStatus(CertifiedCode.UNDER_REVIEW.getCode());
            applicationFormMapper.updateById(applicationForm);
            //补偿,删除物业详情表
            certifiedInfoMapper.deleteById(certifiedInfoId);
            throw new RuntimeException(e);
        }

    }

    @Override
    public void refuseApplication(Integer id) {
        //状态是否为为待审核状态
        ApplicationForm applicationForm = applicationFormMapper.selectById(id);
        if (applicationForm.getApplicationStatus()!=CertifiedCode.UNDER_REVIEW.getCode()){
            throw new CertifiedException(CertifiedExceptionCode.DO_NOT_REPEAT_OPERATION);
        }
        //修改状态
        applicationForm.setApplicationStatus(CertifiedCode.AUDIT_FAILURE.getCode());
        applicationFormMapper.updateById(applicationForm);
    }

    @Override
    public PageInfo<ApplicationForm> passAllApplication(PassAllApplicationParam param) {
        Page<ApplicationForm> page = new Page<ApplicationForm>(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 5 : param.getPageSize());
        QueryWrapper<ApplicationForm> wrapper = new QueryWrapper<ApplicationForm>();
        wrapper.eq("application_status",0);
        applicationFormMapper.selectPage(page, wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setList(page.getRecords());
        pageInfo.setPages(page.getPages());
        System.out.println("----------------");
        System.out.println("----------------");
        System.out.println(pageInfo);
        System.out.println("PageInfo: ");
        System.out.println("List: " + pageInfo.getList());
        System.out.println("Pages: " + pageInfo.getPages());
        return pageInfo;
    }

    @Override
    public ApplicationForm LoadcertifiedBycertifiedInfoName(String certifiedInfoName) {
        QueryWrapper<ApplicationForm> wrapper = new QueryWrapper<ApplicationForm>();
        wrapper.eq("certified_info_name",certifiedInfoName);
        return applicationFormMapper.selectOne(wrapper);
    }
}

